#include <cstdlib>
#include <iostream>
#include "swf.h"
#include "ShapeRecord.h"
#include "StyleChangeRecord.h"
#include "Bits.h"

using namespace std;
using namespace swfstream;

void STRAIGHTEDGERECORD::print() {
	cout << "STRAIGHTEDGERECORD::NumBits:" << NumBits << ",GeneralLineFlag:"
			<< GeneralLineFlag;
	if (!GeneralLineFlag) {
		cout << ",VertLineFlag:" << VertLineFlag;
	}
	if (GeneralLineFlag || (!VertLineFlag)) {
		cout << ",DeltaX:" << DeltaX;
	}
	if (GeneralLineFlag || VertLineFlag) {
		cout << ",DeltaY:" << DeltaY;
	}
	cout << "\n";
}
void StyleChangeRecord::print() {
	cout << "style_change_record::StateNewStyles:" << StateNewStyles
			<< ",StateLineStyle:" << StateLineStyle << ",StateFillStyle1:"
			<< StateFillStyle1 << ",StateFillStyle0:" << StateFillStyle0
			<< ",StateMoveTo:" << StateMoveTo << '\n';
	if (StateMoveTo) {
		cout << "MoveDeltaX:" << MoveDeltaX << ",MoveDeltaY:" << MoveDeltaY
				<< '\n';
	}
	if (StateFillStyle0) {
		cout << "FillStyle0:" << FillStyle0 << '\n';
	}
	if (StateLineStyle) {
		cout << "LineStyle:" << LineStyle << '\n';
	}
	if (StateFillStyle1 || StateNewStyles) {
		cerr << "style_change_record::print not finished\n";
		exit(1);
	}
}

void ShapeRecord::print() {
	printf("shape_record::\n");
}
std::vector<ShapeRecord*>* ShapeRecord::list_from_file(FILE* f,
		uint num_fill_bits, uint num_line_bits) {
	long s = ftell(f);
	std::vector<ShapeRecord*>*r = new std::vector<ShapeRecord*>;
	Bits bits;
	while (true) {
		uint type_flag = bits.read_u(1, f);
		if (type_flag == 0) {
			uint tmp = bits.read_u(5, f);
			if (tmp == 0) { //endshape
				break;
			} else { //style change
				StyleChangeRecord &s = *new StyleChangeRecord;
				s.StateNewStyles = (tmp & 0x10);
				s.StateLineStyle = (tmp & 0x8);
				s.StateFillStyle1 = (tmp & 0x4);
				s.StateFillStyle0 = (tmp & 0x2);
				s.StateMoveTo = (tmp & 0x1);
				if (s.StateMoveTo) {
					s.MoveBits = bits.read_u(5, f);
					s.MoveDeltaX = bits.read_s(s.MoveBits, f);
					s.MoveDeltaY = bits.read_s(s.MoveBits, f);
				}
				if (s.StateFillStyle0) {
					s.FillStyle0 = bits.read_u(num_fill_bits, f);
				}
				if (s.StateFillStyle1) {
					s.FillStyle1 = bits.read_u(num_fill_bits, f);
				}
				if (s.StateLineStyle) {
					s.LineStyle = bits.read_u(num_line_bits, f);
				}
				if (s.StateNewStyles) {
					fprintf(stderr, "unknown type_flag=%u,%u\n", type_flag,
							tmp);
					exit(17);
				}
				r->push_back(&s);
			}
		} else {
			uint tmp = bits.read_u(1, f);
			if (tmp == 0) {
				CURVEDEDGERECORD &s = *new CURVEDEDGERECORD;
				s.NumBits = bits.read_u(4, f);
				s.ControlDeltaX = bits.read_s(s.NumBits + 2, f);
				s.ControlDeltaY = bits.read_s(s.NumBits + 2, f);
				s.AnchorDeltaX = bits.read_s(s.NumBits + 2, f);
				s.AnchorDeltaY = bits.read_s(s.NumBits + 2, f);
				r->push_back(&s);
			} else {
				STRAIGHTEDGERECORD &s = *new STRAIGHTEDGERECORD;
				s.NumBits = bits.read_u(4, f);
				s.GeneralLineFlag = bits.read_u(1, f);
				if (!s.GeneralLineFlag) {
					s.VertLineFlag = bits.read_u(1, f);
				}
				if (s.GeneralLineFlag || (!s.VertLineFlag)) {
					s.DeltaX = bits.read_s(s.NumBits + 2, f);
				}
				if (s.GeneralLineFlag || s.VertLineFlag) {
					s.DeltaY = bits.read_s(s.NumBits + 2, f);
				}
				r->push_back(&s);
//				fprintf(stderr,"unknown type_flag=%u,%u\n",type_flag,tmp);
//				exit(17);
			}
		}
	}
	//printf("length of shape_record list:%ld\n",ftell(f)-s);
	return r;
}

